package com.controller;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.service.UsersService;
import com.service.ArticleService;
import com.service.CateService;
import com.service.GoodsService;
import com.service.CartService;
import com.service.OrdersService;
import com.service.FavService;
import com.service.TopicService;
import com.service.ComplainsService;
import com.entity.Users;
import com.entity.Article;
import com.entity.Cate;
import com.entity.Goods;
import com.entity.Cart;
import com.entity.Orders;
import com.entity.Fav;
import com.entity.Topic;
import com.entity.Complains;
import com.util.PageHelper;
import com.util.VeDate;

//定义为控制器
@Controller
// 设置路径
@RequestMapping("/index")
//前台界面+用户中心
public class IndexController extends BaseController {

	@Autowired
	private UsersService usersService;
	@Autowired
	private ArticleService articleService;
	@Autowired
	private CateService cateService;
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private CartService cartService;
	@Autowired
	private OrdersService ordersService;
	@Autowired
	private FavService favService;
	@Autowired
	private TopicService topicService;
	@Autowired
	private ComplainsService complainsService;

	// TODO Auto-generated method stub
	// 公共方法 提供公共查询数据
	private void front() {
		this.getRequest().setAttribute("title", "校园二手学习用品交易网站");
		//得到所有的商品类型
		List<Cate> cateList = this.cateService.getAllCate();
		this.getRequest().setAttribute("cateList", cateList);
		//得到热卖的商品
		List<Goods> hotList = this.goodsService.getGoodsByHot();
		this.getRequest().setAttribute("hotList", hotList);
	}

	// 首页显示
	@RequestMapping("index.action")
	public String index() {
		this.front();
		//getCateFront():根据cateid来查询，按照cateid排序，cateid跟创建时间有关，取最新创建的前三个类型
		List<Cate> cateList = this.cateService.getCateFront();
		//创建类型的集合，为获取每个类型中的所有商品做准备
		List<Cate> frontList = new ArrayList<Cate>();
		for (Cate cate : cateList) {
			//根据类型id，从后端获取此类型下的所有商品，并封装到goodsList集合中
			List<Goods> goodsList = this.goodsService.getGoodsByCate(cate.getCateid());
			//将每个类型下的商品添加到对应的类型下
			cate.setGoodsList(goodsList);
		    //将每个装好对应商品的每个类型添加到类型集合frontList中
			frontList.add(cate);
		}
		this.getRequest().setAttribute("frontList", frontList);
		return "users/index";
	}

	// 公告
	@RequestMapping("article.action")
	public String article(String number) {
		this.front();
		List<Article> tempList = this.articleService.getAllArticle();
		PageHelper.getIndexPage(tempList, "article", "article", null, 10, number, this.getRequest());
		return "users/article";
	}

	// 阅读公告
	@RequestMapping("read.action")
	public String read(String id) {
		this.front();
		Article article = this.articleService.getArticleById(id);
		article.setHits("" + (Integer.parseInt(article.getHits()) + 1));
		this.articleService.updateArticle(article);
		this.getRequest().setAttribute("article", article);
		return "users/read";
	}

	// 准备登录
	@RequestMapping("preLogin.action")
	public String prelogin() {
		this.front();
		return "users/login";
	}

	// 用户登录
	@RequestMapping("login.action")
	public String login() {
		this.front();
		String username = this.getRequest().getParameter("username");
		String password = this.getRequest().getParameter("password");
		Users u = new Users();
		u.setUsername(username);
		List<Users> usersList = this.usersService.getUsersByCond(u);
		if (usersList.size() == 0) {
			this.getSession().setAttribute("message", "用户名不存在");
			return "redirect:/index/preLogin.action";
		} else {
			Users users = usersList.get(0);
			if ("锁定".equals(users.getStatus())) {
				this.getSession().setAttribute("message", "账户被锁定");
				return "redirect:/index/preLogin.action";
			}
			if (password.equals(users.getPassword())) {
				this.getSession().setAttribute("userid", users.getUsersid());
				this.getSession().setAttribute("username", users.getUsername());
				this.getSession().setAttribute("users", users);
				return "redirect:/index/index.action";
			} else {
				this.getSession().setAttribute("message", "密码错误");
				return "redirect:/index/preLogin.action";
			}
		}
	}

	// 准备注册
	@RequestMapping("preReg.action")
	public String preReg() {
		this.front();
		return "users/register";
	}

	// 用户注册
	@RequestMapping("register.action")
	public String register(Users users) {
		this.front();
		Users u = new Users();
		u.setUsername(users.getUsername());
		//根据前端传来的用户信息中的用户名查询数据库中是否存在此用户
		List<Users> usersList = this.usersService.getUsersByCond(u);
		//如果数据库中不存在此用户，则此用户为新用户，就将其状态该为解锁状态，并且记录下其注册日期，然后将此用户插入到后端用户数据库中
		if (usersList.size() == 0) {
			users.setStatus("解锁");
			users.setRegdate(VeDate.getStringDateShort());
			this.usersService.insertUsers(users);
		} else {
			this.getSession().setAttribute("message", "用户名已存在");
			//返回到注册界面，重新填写信息
			return "redirect:/index/preReg.action";
		}
		//返回到登录界面
		return "redirect:/index/preLogin.action";
	}

	// 退出登录
	@RequestMapping("exit.action")
	public String exit() {
		this.front();
		//删除session会话中的用户信息，然后返回到首页未登录界面
		this.getSession().removeAttribute("userid");
		this.getSession().removeAttribute("username");
		this.getSession().removeAttribute("users");
		return "redirect:/index/index.action";
	}

	// 准备修改密码
	@RequestMapping("prePwd.action")
	public String prePwd() {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		return "users/editpwd";
	}

	// 修改密码
	@RequestMapping("editpwd.action")
	public String editpwd() {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		//从前端得到此用户的userid
		String userid = (String) this.getSession().getAttribute("userid");
		//从前端得到此用户的原密码
		String password = this.getRequest().getParameter("password");
		//从前端得到此用户的新密码
		String repassword = this.getRequest().getParameter("repassword");
		//从数据库中查找到此用户
		Users users = this.usersService.getUsersById(userid);
		//进行判断，得到的原密码与此用户的密码是否相同
		if (password.equals(users.getPassword())) {
			//如果相同，则将原密码修改为新密码
			users.setPassword(repassword);
			//并到底层数据库进行修改
			this.usersService.updateUsers(users);
		} else {
			//如果不相同，就证明旧密码有误，继而将页面跳转到修改密码界面
			this.getSession().setAttribute("message", "旧密码错误");
			return "redirect:/index/prePwd.action";
		}
		this.getSession().setAttribute("message", "修改成功");
		return "redirect:/index/prePwd.action";
	}

	@RequestMapping("usercenter.action")
	public String usercenter() {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		return "users/usercenter";
	}

	@RequestMapping("userinfo.action")
	public String userinfo() {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		String userid = (String) this.getSession().getAttribute("userid");
		this.getSession().setAttribute("users", this.usersService.getUsersById(userid));
		return "users/userinfo";
	}

	@RequestMapping("personal.action")
	public String personal(Users users) {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		this.usersService.updateUsers(users);
		this.getSession().setAttribute("message", "修改成功");
		return "redirect:/index/userinfo.action";
	}

	// 添加商品到购物车
	@RequestMapping("addcart.action")
	public String addcart() {
		this.front();
		//先判断session里有没有用户信息，如果没有，就跳转到登录界面，让用户先登录
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		//从session会话中拿到当前已经登录用户的userid(在此操作中也可以叫买家id)
		String userid = (String) this.getSession().getAttribute("userid");
		//将从前端获取到的商品信息封装到购物车中（商品id,商品购买数量，商品单价，买家id）
		Cart cart = new Cart();
		cart.setAddtime(VeDate.getStringDateShort());
		cart.setGoodsid(getRequest().getParameter("goodsid"));
		cart.setNum(getRequest().getParameter("num"));
		cart.setPrice(getRequest().getParameter("price"));
		cart.setUsersid(userid);
		//将商品填加到购物车中
		this.cartService.insertCart(cart);
		//跳转到本C层的cart.action中进行数据准备
		return "redirect:/index/cart.action";
	}

	// 查看购物车（购买商品时可从addcart.action（添加商品到购物车）跳转过来，也可以在头部信息中直接点击购物车按钮查看）
	@RequestMapping("cart.action")
	public String cart() {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		//从session会话中获取此登录用户的userid
		String userid = (String) this.getSession().getAttribute("userid");
		//根据userid创建此用户的购物车对象（定义此用户的购物车）
		Cart cart = new Cart();
		cart.setUsersid(userid);
		//从后端查询userid对应的所有购物车信息（找到此用户购物车中所有的信息）
		List<Cart> cartList = this.cartService.getCartByCond(cart);
		this.getRequest().setAttribute("cartList", cartList);
		//将查询完的cartList信息返回到cart.jsp中
		return "users/cart";
	}

	// 删除购物车中的商品
	@RequestMapping("deletecart.action")
	public String deletecart(String id) {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		this.cartService.deleteCart(id);
		return "redirect:/index/cart.action";
	}

	// 准备结算
	// 准备结算
	@RequestMapping("preCheckout.action")
	public String preCheckout() {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		String userid = (String) this.getSession().getAttribute("userid");
		Cart cart = new Cart();
		cart.setUsersid(userid);
		//根据带有userid的购物车，查询该购物车中的全部信息
		List<Cart> cartList = this.cartService.getCartByCond(cart);
		if (cartList.size() == 0) {
			this.getRequest().setAttribute("message", "请选购商品");
			return "redirect:/index/cart.action";
		}
		//返回到填写订单信息界面
		return "users/checkout";
	}

	// 结算
	@RequestMapping("checkout.action")
	public String checkout() {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		String userid = (String) this.getSession().getAttribute("userid");
		Cart cart1 = new Cart();
		cart1.setUsersid(userid);
		//获取到该用户购物车中所有的信息
		List<Cart> cartList = this.cartService.getCartByCond(cart1);
		if (cartList.size() == 0) {
			this.getSession().setAttribute("message", "请选购商品");
			return "redirect:/index/cart.action";
		} else {
			//遍历购物车cart
			for (Cart cart : cartList) {
				//根据goodsid，从底层得到该购物车中的对应的商品
				Goods goods = this.goodsService.getGoodsById(cart.getGoodsid());
				//将商品的销售数量改为：销售数量+购物车中商品的数量
				goods.setSellnum("" + (Integer.parseInt(goods.getSellnum()) + Integer.parseInt(cart.getNum())));
				//将商品的库存数改为：库存数-购物车中商品的数量
				goods.setStorage("" + (Integer.parseInt(goods.getStorage()) - Integer.parseInt(cart.getNum())));
			    //在底层修改商品的信息
				this.goodsService.updateGoods(goods);
				//定义订单号
				String ordercode = "PD" + VeDate.getStringId();
				//计算商品总计价格（购买数量*商品单价）
				double total = VeDate
						.getDouble(Double.parseDouble(cart.getNum()) * Double.parseDouble(cart.getPrice()));
				//生成新订单，将订单信息添加到订单对象oeders中（其中地址，联系方式，收货人信息从前端获取）
				Orders orders = new Orders();
				orders.setOrdercode(ordercode);
				orders.setAddress(this.getRequest().getParameter("address"));
				orders.setAddtime(VeDate.getStringDateShort());
				orders.setContact(this.getRequest().getParameter("contact"));
				orders.setGoodsid(goods.getGoodsid());
				orders.setNum(cart.getNum());
				orders.setPrice(cart.getPrice());
				orders.setReceiver(this.getRequest().getParameter("receiver"));
				//此处将订单状态设置为“未付款”，到前端orderlist.jsp界面中用jstl标签库中的<c:if>标签进行判断，如果是“未付款”状态，则将订单列表中商品的操作改为：付款 ，取消
				orders.setStatus("未付款");
				orders.setTotal("" + total);
				orders.setUsersid(cart.getUsersid());
				//将此订单信息添加到数据库中（添加到订单表中）
				this.ordersService.insertOrders(orders);
				//同时将此购物车信息从数据库中删除
				this.cartService.deleteCart(cart.getCartid());
			}
		}
		//携带结果跳转到showOrders.action中
		return "redirect:/index/showOrders.action";
	}

	// 查看订单
	@RequestMapping("showOrders.action")
	public String showOrders(String number) {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		String userid = (String) this.getSession().getAttribute("userid");
		Orders orders = new Orders();
		orders.setUsersid(userid);
		//根据userid查看此人的全部订单信息
		List<Orders> tempList = this.ordersService.getOrdersByCond(orders);
		//将查询到的订单信息做分页显示，并准备将结果返回给前端
		PageHelper.getIndexPage(tempList, "orders", "showOrders", null, 10, number, this.getRequest());
		//携带查询出来的订单信息tempList等结果，返回到前端orderlist.jsp界面中
		return "users/orderlist";
	}
	//我的销售（传递一个当前页码）
	@RequestMapping("mySell.action")
	public String mySell(String number) {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		//获取此卖家的用户id
		String userid = (String) this.getSession().getAttribute("userid");
		Orders orders = new Orders();
		orders.setSellerid(userid);
		//根据卖家的用户id，查询此卖家所有销售订单的集合
		List<Orders> tempList = this.ordersService.getOrdersByCond(orders);
		//并将查询到的结果集合进行分页显示
		PageHelper.getIndexPage(tempList, "orders", "mySell", null, 10, number, this.getRequest());
		//跳转到我的销售sorderlist.jsp界面中
		return "users/sorderlist";
	}
	
	//卖家已发货（传递过来订单id）
	@RequestMapping("send.action")
	public String send(String id) {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		//根据传递过来的订单id，在底层将此订单状态改为已发货
		Orders orders = this.ordersService.getOrdersById(this.getRequest().getParameter("id"));
		orders.setStatus("已发货");
		this.ordersService.updateOrders(orders);
		//修改完毕后重定向到mySell.action中
		return "redirect:/index/mySell.action";
	}

	// 准备付款
	@RequestMapping("prePay.action")
	public String prePay(String id) {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		//接收从前端orderlist.jsp中传递过来的订单id
		this.getRequest().setAttribute("id", id);
		Orders orders = this.ordersService.getOrdersById(id);
		this.getRequest().setAttribute("orders", orders);
		//跳转到支付宝付款界面pay.jsp
		return "users/pay";
	}

	// 付款
	@RequestMapping("pay.action")
	public String pay(String id) {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		//获取到前端传递过来的订单id，根据订单id查到此订单
		Orders orders = this.ordersService.getOrdersById(this.getRequest().getParameter("id"));
		//并将此订单的状态改为已付款
		orders.setStatus("已付款");
		//传递到底层修改订单状态为已付款
		this.ordersService.updateOrders(orders);
		return "redirect:/index/showOrders.action";
	}

	// 确认收货
	@RequestMapping("over.action")
	public String over(String id) {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		//根据订单id查询到此订单，并将订单状态修改为已收货
		Orders orders = this.ordersService.getOrdersById(this.getRequest().getParameter("id"));
		orders.setStatus("已收货");
		this.ordersService.updateOrders(orders);
		//返回到showOrders.action中，从而返回到我的订单页中
		return "redirect:/index/showOrders.action";
	}

	// 取消订单
	@RequestMapping("cancel.action")
	public String cancel(String id) {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		Orders orders = this.ordersService.getOrdersById(this.getRequest().getParameter("id"));
		orders.setStatus("已取消");
		this.ordersService.updateOrders(orders);
		return "redirect:/index/showOrders.action";
	}

	// 按分类查询（前端更多）
	@RequestMapping("cate.action")
	public String cate(String id, String number) {
		this.front();
		//将获取的cateid和商品标志位为“上架”的商品状态，添加进goods对象中
		Goods goods = new Goods();
		goods.setCateid(id);
		goods.setStatus("上架");
		//根据cateid查询状态为“上架”的所有对应类型下的商品（查询此类型所有上架的物品）
		List<Goods> tempList = this.goodsService.getGoodsByCond(goods);
		//将查询出来的每个类型下的所有商品做分页显示后传递回前端 users/list商品列表界面
		PageHelper.getIndexPage(tempList, "goods", "cate", id, 12, number, this.getRequest());
		//跳到相应类型下的商品列表中list.jsp
		return "users/list";
	}

	// 全部产品
	@RequestMapping("all.action")
	public String all(String number) {
		this.front();
		Goods goods = new Goods();
		goods.setStatus("上架");
		List<Goods> tempList = this.goodsService.getGoodsByCond(goods);
		PageHelper.getIndexPage(tempList, "goods", "all", null, 12, number, this.getRequest());
		return "users/list";
	}

	// 查询商品
	@RequestMapping("query.action")
	public String query(String name) {
		this.front();
		Goods goods = new Goods();
		goods.setGoodsname(name);
		goods.setStatus("上架");
		List<Goods> goodsList = this.goodsService.getGoodsByLike(goods);
		this.getRequest().setAttribute("goodsList", goodsList);
		return "users/list";
	}

	// 商品详情（根据前端传来的goodsid查询对应的商品详细信息）
	@RequestMapping("detail.action")
	public String detail(String id) {
		this.front();
		//根据传过来的goodsid查询对应的商品信息
		Goods goods = this.goodsService.getGoodsById(id);
		//先将此商品的点击量加1
		goods.setHits("" + (Integer.parseInt(goods.getHits()) + 1));
		this.goodsService.updateGoods(goods);
		this.getRequest().setAttribute("goods", goods);
		//新建评价topic对象
		Topic topic = new Topic();
		//将此商品id添加到评价对象中
		topic.setGoodsid(id);
		//查询并显示这个商品所有被评价的内容信息
		List<Topic> topicList = this.topicService.getTopicByCond(topic);
		this.getRequest().setAttribute("topicList", topicList);
		//将评价集合中的评价个数传到前端
		this.getRequest().setAttribute("tnum", topicList.size());
		return "users/detail";
	}

	// 添加收藏
	@RequestMapping("addfav.action")
	public String addfav(Fav fav) {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		String userid = (String) getRequest().getSession().getAttribute("userid");
		fav.setAddtime(VeDate.getStringDateShort());
		fav.setGoodsid(getRequest().getParameter("id"));
		fav.setUsersid(userid);
		this.favService.insertFav(fav);
		return "redirect:/index/myfav.action";
	}

	// 我的收藏
	@RequestMapping("myfav.action")
	public String myfav(String number) {
		this.front();
		if (getRequest().getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		String userid = (String) this.getSession().getAttribute("userid");
		Fav fav = new Fav();
		fav.setUsersid(userid);
		List<Fav> favList = this.favService.getFavByCond(fav);
		PageHelper.getIndexPage(favList, "fav", "myfav", null, 10, number, this.getRequest());
		return "users/myfav";
	}

	// 删除收藏
	@RequestMapping("deletefav.action")
	public String deletefav() {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		this.favService.deleteFav(this.getRequest().getParameter("id"));
		return "redirect:/index/myfav.action";
	}
	
	//准备商品，返回到发布商品界面
	@RequestMapping("preGoods.action")
	public String preGoods() {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		return "users/addGoods";
	}
	
	//发布商品
	@RequestMapping("addGoods.action")
	public String addGoods(Goods goods) {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		//从前端获取到卖家id
		String userid = (String) this.getSession().getAttribute("userid");
		goods.setAddtime(VeDate.getStringDateShort());
		goods.setUsersid(userid);
		//初始化点击量为0
		goods.setHits("0");
		//初始化销售数量为0
		goods.setSellnum("0");
		//初始化商品状态为下架，目的是为了让后端管理员审核通过后，由管理员决定上架与否
		goods.setStatus("下架");
		//将发布的商品添加到后端数据库中
		this.goodsService.insertGoods(goods);
		//返回到preGoods.action中，从而返回到发布商品界面
		return "redirect:/index/preGoods.action";
	}
	
	//我的商品（我上架的商品）
	@RequestMapping("myGoods.action")
	public String myGoods(String number) {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		//从前端获取到卖家id（userid）
		String userid = (String) this.getSession().getAttribute("userid");
		Goods goods = new Goods();
		goods.setUsersid(userid);
		//查看此用户所有上架的商品，并在前端做分页显示
		List<Goods> tempList = this.goodsService.getGoodsByCond(goods);
		PageHelper.getIndexPage(tempList, "goods", "myGoods", null, 10, number, this.getRequest());
		//返回到我的商品界面
		return "users/myGoods";
	}
	
	//准备编辑我的商品（传递过来goodsid）
	@RequestMapping("preEdit.action")
	public String preEdit(String id) {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		//通过前端传递过来的goodsid查询到此商品
		Goods goods = this.goodsService.getGoodsById(id);
		//将此商品返回给前端编辑界面
		this.getRequest().setAttribute("goods", goods);
		return "users/editGoods";
	}
	
	//修改已经发布的商品
	@RequestMapping("updateGoods.action")
	public String updateGoods(Goods goods) {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		//将从前端传递过来的商品信息传到底层进行修改
		this.goodsService.updateGoods(goods);
		//修改后重定向到myGoods.action，继而跳转到我的商品界面myGoods.jsp中
		return "redirect:/index/myGoods.action";
	}
	
	//删除已经发布的商品
	@RequestMapping("deleteGoods.action")
	public String deleteGoods(String id) {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		this.goodsService.deleteGoods(id);
		//删除后重定向到myGoods.action，继而跳转到我的商品界面myGoods.jsp中
		return "redirect:/index/myGoods.action";
	}
	
	//评价商品
	@RequestMapping("preTopic.action")
	public String preTopic(String id) {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		//根据传递过来的订单id查询到此订单
		Orders orders = this.ordersService.getOrdersById(id);
		//并将订单id和查询到的订单返回给前端界面
		this.getRequest().setAttribute("id", id);
		this.getRequest().setAttribute("orders", orders);
		return "users/addTopic";
	}
	
	//订单评价
	@RequestMapping("addTopic.action")
	public String addTopic(String id) {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		//根据传递过来的订单id查询到该订单，并将订单状态修改为已评价
		Orders orders = this.ordersService.getOrdersById(id);
		orders.setStatus("已评价");
		this.ordersService.updateOrders(orders);
		//将评价信息封装到评价对象topic中
		Topic topic = new Topic();
		topic.setAddtime(VeDate.getStringDateShort());
		topic.setUsersid(orders.getUsersid());
		topic.setGoodsid(orders.getGoodsid());
		topic.setOrdersid(orders.getOrdersid());
		topic.setNum(this.getRequest().getParameter("num"));
		topic.setContents(this.getRequest().getParameter("contents"));
		topic.setOrdersid(id);
		//将评价信息插入到底层数据库中
		this.topicService.insertTopic(topic);
		//然后重定向到showOrders.action中，继而跳转到订单信息列表界面orderlist.jsp中
		return "redirect:/index/showOrders.action";
	}
	
	//跳转到添加反馈界面
	@RequestMapping("preComplains.action")
	public String preComplains() {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		return "users/addComplains";
	}
	
	//编写意见反馈信息
	@RequestMapping("addComplains.action")
	public String addComplains(Complains complains) {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		//从前端获取该登录用户的userid
		String userid = (String) this.getSession().getAttribute("userid");
		//给传递过来的反馈对象complains赋值，发布日期
		complains.setAddtime(VeDate.getStringDateShort());
		//赋值反馈状态为未回复
		complains.setStatus("未回复");
		//赋值用户id
		complains.setUsersid(userid);
		//赋值管理员回复为-
		complains.setReps("-");
		//将反馈信息插入到底层数据库中
		this.complainsService.insertComplains(complains);
		//然后重定向到preComplains.action
		return "redirect:/index/preComplains.action";
	}
	
	//我的意见反馈（显示界面）
	@RequestMapping("myComplains.action")
	public String myComplains(String number) {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		//从前端获取该登录用户的userid
		String userid = (String) this.getSession().getAttribute("userid");
		Complains complains = new Complains();
		complains.setUsersid(userid);
		//查询到该用户的所有反馈信息，并做分页显示
		List<Complains> complainsList = this.complainsService.getComplainsByCond(complains);
		PageHelper.getIndexPage(complainsList, "complains", "myComplains", null, 10, number, this.getRequest());
		//跳转到我的意见反馈界面进行分页显示
		return "users/myComplains";
	}
	// TODO Auto-generated method stub
}
